package com.itg.vms.servlet;

import cn.dsna.util.images.ValidateCode;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/code")
public class CodeServlet extends BaseServlet{

    /**
     * 生成图形验证码，暂时先不用去在前端适配，先放在这里
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void createCode(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.创建生成验证码对象: 参数说明: codeCount(验证码数字的个数),lineCount(干扰线条数量)
        // new ValidateCode(width,height,codeCount,lineCount)
        ValidateCode validateCode = new ValidateCode(100, 35, 5, 10);
        // 获取验证码数字
        String code = validateCode.getCode();
        System.out.println(code);
        // 2.将验证码放入session中
        req.getSession().setAttribute("code",code);
        // 3.写回给客户端
        ServletOutputStream outputStream = resp.getOutputStream();
        validateCode.write(outputStream);
    }
}
